package com.orcamento.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.orcamento.entity.Event;

public class EventDao extends AbstractDao<Event> {

	public EventDao() {
		super(Event.class);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public Event save(Event event) {
		return saveEntity(event);
	}

	@SuppressWarnings("unchecked")
	public List<Event> findAllByAccount(Long accountId) {
		Criteria criteria = session().createCriteria(Event.class, "event");
		criteria.createAlias("fund", "fund", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("account", "account");
		criteria.add( Restrictions.isNull("fund.event") );
		criteria.add( Restrictions.eq("account.id", accountId) );
		criteria.addOrder( Order.asc("event.name") );
		return (List<Event>) criteria.list();
		
	}

	public Event findByIdAndAccount(Long id, Long accountId) {
		return super.findByIdAndAccount(id, accountId);
	}

}
